<template>
  <div class="main-content">
    <div class="left-content">
      <!-- <img src="~@/assets/bg_logo.png" class="logo" alt="logo" /> -->
      <div class="system-name">信息高铁联邦管理系统</div>
    </div>
    <div class="right-content">
      <a-form id="formLogin" ref="formLogin" class="right-content-login" :form="form" @submit="handleSubmit">
        <a-row class="tilte">
          欢迎登录
        </a-row>
        <a-row>
          <a-col :xl="24" :lg="24" :md="24" :sm="24" :xs="24">
            <a-form-item class="form-item-bottom">
              <a-input
                id="input-box"
                v-decorator="[
                  'username',
                  { rules: [{ required: true, message: '请输入账号或邮箱地址' }, { validator: handleUsernameOrEmail }] }
                ]"
                class="account"
                type="text"
                placeholder=""
                autocomplete="off"
              >
                <span slot="prefix" class="little">账号</span>
              </a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :xl="24" :lg="24" :md="24" :sm="24" :xs="24">
            <a-form-item class="form-item-bottom">
              <a-input-password
                v-decorator="[
                  'password',
                  { rules: [{ required: true, message: '请输入密码' }], validateTrigger: 'blur', initialValue: '' }
                ]"
                class="account"
                placeholder=""
                autocomplete="off"
              >
                <span slot="prefix" class="little">密码</span>
              </a-input-password>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="verificationOut">
          <a-form-item>
            <a-input
              v-decorator="['verification', { rules: [{ required: true, message: '请输入验证码' }] }]"
              type="text"
              placeholder=""
              class="verification"
              autocomplete="off"
            >
              <span slot="prefix" class="little">验证码</span>
            </a-input>
          </a-form-item>
          <img :src="'data:image/png;base64,' + captchaImg" alt="" class="verificationImg" />
        </a-row>
        <a-form-item style="margin-top:2px;">
          <a-button
            type="primary"
            htmlType="submit"
            class="login-button"
            :loading="state.loginBtn"
            :disabled="state.loginBtn"
          >
            确定
          </a-button>
        </a-form-item>
      </a-form>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { timeFix } from '@/utils/util'
import { captcha } from '@/api/login'
export default {
  data() {
    return {
      customActiveKey: 'tab1',
      loginBtn: false,
      loginType: 0,
      isLoginError: false,
      form: this.$form.createForm(this),
      state: {
        time: 60,
        loginBtn: false,
        // login type: 0 email, 1 username, 2 telephone
        loginType: 0,
        smsSendBtn: false
      },
      captchaImg: null,
      uuid: ''
    }
  },
  created() {
    captcha({})
      .then(response => {
        this.captchaImg = response.data.captcha || null
        this.uuid = response.data.uuid || ''
      })
      .catch(function(error) {
        console.log(error)
      })
  },
  methods: {
    ...mapActions(['Login', 'Logout']),
    // handler
    handleUsernameOrEmail(rule, value, callback) {
      const { state } = this
      const regex = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/
      if (regex.test(value)) {
        state.loginType = 0
      } else {
        state.loginType = 1
      }
      callback()
    },
    handleTabClick(key) {
      this.customActiveKey = key
      // this.form.resetFields()
    },
    handleSubmit(e) {
      const that = this
      e.preventDefault()
      const {
        form: { validateFields },
        state,
        customActiveKey,
        Login
      } = this

      state.loginBtn = true
      const validateFieldsKey =
        customActiveKey === 'tab1' ? ['username', 'password', 'verification'] : ['mobile', 'captcha']

      validateFields(validateFieldsKey, { force: true }, (err, values) => {
        if (!err) {
          const loginParams = {}
          loginParams.username = values.username
          loginParams.password = values.password
          loginParams.uuid = that.uuid
          loginParams.verCode = values.verification
          console.log('login form', loginParams)
          Login(loginParams)
            .then(res => this.loginSuccess(res))
            .catch(err => this.requestFailed(err))
            .finally(() => {
              state.loginBtn = false
            })
        } else {
          setTimeout(() => {
            state.loginBtn = false
          }, 600)
        }
      })
    },
    loginSuccess(res) {
      console.log(res)
      this.$router.push({ path: '/' })
      // 延迟 1 秒显示欢迎信息
      setTimeout(() => {
        this.$notification.success({
          message: '欢迎',
          description: `${timeFix()}，欢迎回来`
        })
      }, 1000)
      this.isLoginError = false
    },
    requestFailed(err) {
      this.isLoginError = true
      this.$notification['error']({
        message: '错误',
        description: ((err.response || {}).data || {}).message || '请求出现错误，请稍后再试',
        duration: 4
      })
    }
  }
}
</script>

<style lang="less" scoped>
.main-content {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.left-content {
  // width: 390px;
  height: 100%;
  margin-right: 40px;
  .left-content-logo {
    width: 253px;
    height: 62px;
  }
  .system-name {
    margin-top: 38px;
    font-size: 30px;
    color: #b6caf6;
  }
}
.right-content {
  border-left: 2px solid #124771;
  padding-left: 70px;
  .form-item-bottom {
    border-bottom: 1px solid #425783;
  }
  /deep/ .ant-input {
    background-color: rgba(255, 255, 255, 0);
    padding-left: 65px;
    color: #8ea3d1;
    border: none;
  }
  /deep/ .ant-input:hover {
    background-color: rgba(255, 255, 255, 0);
    border-color: rgba(255, 255, 255, 0);
  }
  /*密码是否显示*/
  /deep/ .ant-input-suffix {
    color: #8ea3d1;
    right: -12px;
    svg {
      width: 24px;
      height: 18px;
    }
  }
  /*输入框提示*/
  /deep/ .little {
    font-size: 14px;
    color: #8ea3d1;
  }
  /deep/ .ant-form {
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    margin: 9px 0 13px 0;
    padding: 0;
    color: rgba(0, 0, 0, 0.65);
    font-size: 14px;
    font-variant: tabular-nums;
    line-height: 1.5;
    list-style: none;
    -webkit-font-feature-settings: 'tnum';
    font-feature-settings: 'tnum';
  }
  /*form表单每一项*/
  /deep/ .ant-form-item {
    margin-bottom: 0px;
  }
  /deep/ .ant-row {
    margin-bottom: 6px;
  }
  /*输入框的文字边距*/
  /deep/ .ant-input-prefix {
    left: 0;
  }
  .tilte {
    font-size: 22px;
    margin-bottom: 18px;
    color: #b6caf6;
  }
  .account {
    width: 240px;
  }
  button.login-button {
    margin-top: 15px;
    width: 240px;
    height: 39px;
    background-color: #2593c9;
    border-radius: 21px;
    font-size: 16px;
  }
  .verificationOut {
    display: flex;
    align-items: center;
    border-bottom: 1px solid #425783;
    .verification {
      width: 125px;
      margin-right: 6px;
    }
    .verificationImg {
      width: 115px;
      height: 37px;
      margin: -6px 0 6px 0;
    }
  }
}
</style>
